package com.eliaukjq.jihe;

/**
 * @Author : 佩萁
 * Description :
 * @Create : 2024/7/12 - 21:12
 */

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class Example {
    public static void main(String[] args) {
        // 创建一个 HashMap
        Map<String, Integer> studentGrades = new HashMap<>();
        studentGrades.put("Alice", 90);
        studentGrades.put("Bob", 85);
        studentGrades.put("Charlie", 88);

        // 获取映射的条目集，并遍历它
        Set<Map.Entry<String, Integer>> entries = studentGrades.entrySet();
        for (Map.Entry<String, Integer> entry : entries) {
            // 打印每个条目的键和值
            System.out.println("Student: " + entry.getKey() + ", Grade: " + entry.getValue());
        }

        // 假设我们需要更新 Charlie 的成绩
        for (Map.Entry<String, Integer> entry : entries) {
            if ("Charlie".equals(entry.getKey())) {
                entry.setValue(92);  // 更新成绩
            }
        }

        // 再次打印更新后的映射
        System.out.println("Updated Grades:");
        for (Map.Entry<String, Integer> entry : entries) {
            System.out.println("Student: " + entry.getKey() + ", Grade: " + entry.getValue());
        }
    }
}
